package com.xsprite.managesys.servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class BaseServlet
 */
public class BaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest req;
	private HttpServletResponse resp;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.req = req;
		this.resp = resp;
		initCharacterEncoding();
	}
	
	private void initCharacterEncoding() throws UnsupportedEncodingException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
	}
	
	protected boolean isParameterNull(String key) {
		String val = req.getParameter(key);
		if(val == null){
			return true;
		}
		return false;
	}
	
	protected String queryParam(String key){
		return req.getParameter(key);
	}
	
	protected void forwardTo(String path) throws ServletException, IOException{
		RequestDispatcher dispatcher = req.getRequestDispatcher(path);
		dispatcher.forward(req, resp);
	}
	
	protected void putParaToRequestAttr(String name, Object obj){
		req.setAttribute(name, obj);
	}

	public HttpServletRequest getReq() {
		return req;
	}

	public HttpServletResponse getResp() {
		return resp;
	}
}
